package com.bnz.service.impl;

import com.bnz.entity.Classes;
import com.bnz.entity.PageResult;
import com.bnz.entity.Student;
import com.bnz.entity.StudentVo;
import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.service.StudentService;
import com.bnz.util.MybatisUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    //1. 学生列表
    @Override
    public List<Student> findAll() {

        try (SqlSession sqlSession = MybatisUtil.getSqlSession(true)){
            //1.1 查询所有学生
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
            List<Student> students = mapper.findAll();
            //1.2 遍历学生列表，根据学生的班级编号找到所在班级，从而找到班级名称
            for (Student student : students) {
                //1.2.1 得到班级编号
                Integer cid = student.getCid();
                //1.2.2 根据班级id查询班级
                Classes classes = classesMapper.findById(cid);
                //1.2.3 为当前学生设置所在班级名称
                student.setCname(classes.getCname());
            }
            //1.3 返回学生列表
            return students;
        }
    }

    // 2. 分页查询
    @Override
    public PageResult<Student> findByPage(int page, int pageSize) {
        //2.1 开始分页
        PageHelper.startPage(page,pageSize);
        //2.2 开始查询，对所有的学生列表进行查询
        List<Student> students = this.findAll();
        //2.3 将学生集合转换为Page<Student>对象
        Page<Student> studentPage = (Page<Student>) students;
        //2.4 返回分页结果对象
        return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
    }

    // 3. 条件查询带着分页
    @Override
    public PageResult<Student> search(int page, int pageSize, StudentVo studentVo) {
        //3.1 开始分页
        PageHelper.startPage(page,pageSize);
        //3.2 开始查询，对所有的学生列表进行查询
        List<Student> students = this.findByWords(studentVo);
        //3.3 将学生集合转换为Page<Student>对象
        Page<Student> studentPage = (Page<Student>) students;
        //3.4 返回分页结果对象
        return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
    }

    // 4. 条件查询
    @Override
    public List<Student> findByWords(StudentVo studentVo) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession(true)){
            // 4.1 得到所有的mapper
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
            // 4.2 根据信息查询学生
            List<Student> students = mapper.findByWords(studentVo);
            // 4.3 遍历学生列表，根据学生的班级编号找到所在班级，从而找到班级名称
            for (Student student : students) {
                //4.3.1 得到班级编号
                Integer cid = student.getCid();
                //4.3.2 根据班级id查询班级
                Classes classes = classesMapper.findById(cid);
                //4.3.3 为当前学生设置所在班级名称
                student.setCname(classes.getCname());
            }
            return students;
        }
    }

    //5. 添加学生
    @Override
    public void save(Student student) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession(true)){
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            mapper.insert(student);
        }
    }

    //6. 修改学生
    @Override
    public void update(Student student) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession(true)){
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            mapper.update(student);
        }
    }
    //7. 删除学生
    @Override
    public void delete(List<String> ids) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession(true)){
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            mapper.delete(ids);
        }
    }
}
